﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class EditorCreateMap: EditorWindow
{
    static EditorCreateMap ecmWindow;
    [MenuItem("Window/创建六边形网格地图")]
    static void ShowWindow()
    {
        ecmWindow = GetWindow<EditorCreateMap>();
        ecmWindow.Show();
    }
    int edgeCount = 6;
    float edgeLength = 1f;
    Material mapMaterial;
    Material cellMaterial;
    private void OnGUI()
    {
        edgeCount = EditorGUILayout.IntSlider("六边形地图边的个数", edgeCount, 1, 200);
        edgeLength = EditorGUILayout.Slider("六边形网格的边长", edgeLength, 0.1f, 10f);
        mapMaterial = EditorGUILayout.ObjectField("地图材质", mapMaterial, typeof(Material), false) as Material;
        cellMaterial = EditorGUILayout.ObjectField("六边形单元格材质", cellMaterial, typeof(Material), false) as Material;
        if (GUILayout.Button("创建地图"))
        {
            //创建地图网格
            Mesh mapMesh = new Mesh();
            MapData mapData = new MapData();
            HexagnoalMapCreator.CreateHexagnoalMap(edgeCount, edgeLength, mapData);
            mapMesh.vertices = mapData.Vertices;
            mapMesh.triangles = mapData.Triangles;
            mapMesh.RecalculateBounds();
            mapMesh.RecalculateNormals();
            AssetDatabase.CreateAsset(mapMesh, "Assets/New HexMapMesh.asset");
            //创建一个单元格网格
            Mesh cellMesh = new Mesh();
            mapData = new MapData();
            HexagnoalMapCreator.CreateHexagnoalMap(1, edgeLength, mapData);
            cellMesh.vertices = mapData.Vertices;
            cellMesh.triangles = mapData.Triangles;
            cellMesh.RecalculateBounds();
            cellMesh.RecalculateNormals();
            AssetDatabase.CreateAsset(cellMesh, "Assets/New HexMapCellMesh.asset");
            //创建地图数据
            HexagnoalMapData heagonalMapData = new HexagnoalMapData();
            heagonalMapData.CellMesh = cellMesh;
            heagonalMapData.MapMesh = mapMesh;
            heagonalMapData.EdgeCount = edgeCount;
            heagonalMapData.OuterRadius = edgeLength;
            heagonalMapData.CellMaterial = cellMaterial;
            heagonalMapData.MapMaterial = mapMaterial;

            AssetDatabase.CreateAsset(heagonalMapData, "Assets/New HexMapData.asset");

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh(ImportAssetOptions.Default);
        }
    }

}
